{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 计算列",
   "id": "fa5c2ae4c61b9237"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:01:11.321824Z",
     "start_time": "2025-09-11T06:01:10.167325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day13/file/'"
   ],
   "id": "6dfc83833db4ee22",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T09:09:31.614249Z",
     "start_time": "2025-09-10T09:09:31.592777Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '计算列.xlsx',index_col='序号')\n",
    "\n",
    "df['销售金额'] = df['单价'] * df['销售数量']\n",
    "\n",
    "# 添加折扣列，所有的折扣为 单价 * 0.8\n",
    "df['折扣价'] = df['单价'] * 0.8\n",
    "print(df)"
   ],
   "id": "d616152fd9ee3b97",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   商品名称  单价  销售数量  销售金额  折扣价\n",
      "序号                          \n",
      "1    香蕉   5    20   100  4.0\n",
      "2    苹果   6    15    90  4.8\n",
      "3     梨   3    18    54  2.4\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T09:13:50.766803Z",
     "start_time": "2025-09-10T09:13:50.751913Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# csv文件计算列\n",
    "df = pd.read_csv(path + '计算列.csv',index_col='序号',encoding='UTF-8')\n",
    "\n",
    "df['销售金额'] = df['销售数量'] * df['单价']\n",
    "\n",
    "print(df)\n"
   ],
   "id": "b4d7f03c6d0efb6e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   商品名称  单价  销售数量  销售金额\n",
      "序号                     \n",
      "1    香蕉   5    20   100\n",
      "2    苹果   6    15    90\n",
      "3     梨   3    18    54\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 当你不想全部都计算，只想计算一部分行的时候，需要使用For循环",
   "id": "816b101673d4c4dd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T09:20:46.416749Z",
     "start_time": "2025-09-10T09:20:46.390951Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '计算列.xlsx',index_col='序号')\n",
    "\n",
    "print(df.iloc[0,0])\n",
    "for i in range(1,3):\n",
    "    # df.loc[i,'销售金额'] = df.loc[i,'单价'] * df.loc[i,'销售数量']\n",
    "    df.at[i,'销售金额'] = df.at[i,'单价'] * df.at[i,'销售数量']\n",
    "\n",
    "print(df)"
   ],
   "id": "1418a47ed8f2169a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "香蕉\n",
      "   商品名称  单价  销售数量   销售金额\n",
      "序号                      \n",
      "1    香蕉   5    20  100.0\n",
      "2    苹果   6    15   90.0\n",
      "3     梨   3    18    NaN\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 使用apply函数 ，自动遍历整个Series或者整个df",
   "id": "b97234e97007248f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T09:24:15.477269Z",
     "start_time": "2025-09-10T09:24:15.439137Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '计算列.xlsx',index_col='序号')\n",
    "print(df)\n",
    "\n",
    "# 涨价的功能，所有商品涨价1元\n",
    "def add_price(money):\n",
    "    return money + 1\n",
    "\n",
    "df['单价'] = df['单价'].apply(add_price)\n",
    "print('涨价后')\n",
    "print(df)\n"
   ],
   "id": "c3fbd9fd20e52620",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   商品名称  单价  销售数量  销售金额\n",
      "序号                     \n",
      "1    香蕉   5    20   NaN\n",
      "2    苹果   6    15   NaN\n",
      "3     梨   3    18   NaN\n",
      "涨价后\n",
      "   商品名称  单价  销售数量  销售金额\n",
      "序号                     \n",
      "1    香蕉   6    20   NaN\n",
      "2    苹果   7    15   NaN\n",
      "3     梨   4    18   NaN\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T09:26:02.954324Z",
     "start_time": "2025-09-10T09:26:02.936571Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# apply中使用匿名函数，\n",
    "print(df)\n",
    "\n",
    "# 所有商品涨价10块\n",
    "df['单价'] = df['单价'].apply(lambda price:price + 10)\n",
    "print('涨价后')\n",
    "print(df)"
   ],
   "id": "11e049d6b7543a59",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   商品名称  单价  销售数量  销售金额\n",
      "序号                     \n",
      "1    香蕉   6    20   NaN\n",
      "2    苹果   7    15   NaN\n",
      "3     梨   4    18   NaN\n",
      "涨价后\n",
      "   商品名称  单价  销售数量  销售金额\n",
      "序号                     \n",
      "1    香蕉  16    20   NaN\n",
      "2    苹果  17    15   NaN\n",
      "3     梨  14    18   NaN\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:08:10.256929Z",
     "start_time": "2025-09-11T06:08:10.242748Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 少数民族加分5分，显示到最终分数\n",
    "data = pd.read_excel(path + '成绩.xlsx')\n",
    "print(data)\n",
    "\n",
    "data['加分'] = data['民族'].apply(lambda x: 5 if x !='汉' else 0)\n",
    "data['最终成绩'] = data['加分'] + data['总分']\n",
    "print(data)\n",
    "\n",
    "# 使用Python内置函数 len 获取姓名长度\n",
    "data['姓名字符数'] = data['姓名'].apply(len)\n",
    "print(data)"
   ],
   "id": "4a4c283b4f0d84b9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   序号  姓名 民族   总分\n",
      "0   1  张三  汉  591\n",
      "1   2  李四  满  589\n",
      "2   3  王五  回  587\n",
      "   序号  姓名 民族   总分  加分  最终成绩\n",
      "0   1  张三  汉  591   0   591\n",
      "1   2  李四  满  589   5   594\n",
      "2   3  王五  回  587   5   592\n",
      "   序号  姓名 民族   总分  加分  最终成绩  姓名字符数\n",
      "0   1  张三  汉  591   0   591      2\n",
      "1   2  李四  满  589   5   594      2\n",
      "2   3  王五  回  587   5   592      2\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:23:07.124078Z",
     "start_time": "2025-09-11T06:23:07.112878Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# apply遍历每一个元素执行指定的函数\n",
    "import  numpy as np\n",
    "arr = [\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(arr,columns=list('abc'),index=list('xyz'))\n",
    "print(df)\n",
    "\n",
    "# 对所有的元素进行平方\n",
    "df2 = df.apply(np.square)\n",
    "print(df2)\n",
    "\n",
    "print('=' * 30)\n",
    "\n",
    "# x 行进行平方运算：\n",
    "df3 = df.apply(lambda x:np.square(x) if x.name == 'x' else x ,axis=1)\n",
    "print(df3)\n",
    "\n",
    "print('=' * 30)\n",
    "# a列进行平方运算\n",
    "df4 = df.apply(lambda a:np.square(a) if a.name == 'a' else a)\n",
    "print(df4)\n",
    "\n",
    "print('=' * 30)\n",
    "# a和 c列进行平方\n",
    "df5 = df.apply(lambda a:np.square(a) if a.name in ['a','c'] else a)\n",
    "print(df5)\n",
    "\n",
    "# x,y 行进行平方\n",
    "df6 = df.apply(lambda a:np.square(a) if a.name in ['x','y'] else a,axis=1)\n",
    "print(df6)"
   ],
   "id": "27153536512098e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a  b  c\n",
      "x  1  2  3\n",
      "y  4  5  6\n",
      "z  7  8  9\n",
      "    a   b   c\n",
      "x   1   4   9\n",
      "y  16  25  36\n",
      "z  49  64  81\n",
      "==============================\n",
      "   a  b  c\n",
      "x  1  4  9\n",
      "y  4  5  6\n",
      "z  7  8  9\n",
      "==============================\n",
      "    a  b  c\n",
      "x   1  2  3\n",
      "y  16  5  6\n",
      "z  49  8  9\n",
      "==============================\n",
      "    a  b   c\n",
      "x   1  2   9\n",
      "y  16  5  36\n",
      "z  49  8  81\n",
      "    a   b   c\n",
      "x   1   4   9\n",
      "y  16  25  36\n",
      "z   7   8   9\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 日期计算",
   "id": "8f4d66893d4a3700"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:33:07.891867Z",
     "start_time": "2025-09-11T06:33:07.876721Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '日期.xlsx',index_col='序号')\n",
    "\n",
    "print(df)\n",
    "\n",
    "df['间隔天数'] = df['结束日期'] - df['起始日期']\n",
    "df['间隔天数'] = df['间隔天数'].apply(lambda x:x.days)\n",
    "print(df)\n",
    "\n",
    "\n",
    "\n"
   ],
   "id": "2e03c1785ce3636",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         起始日期       结束日期\n",
      "序号                      \n",
      "1  2004-05-06 2025-09-11\n",
      "2  2004-07-08 2025-09-11\n",
      "3  2024-09-06 2025-09-11\n",
      "4  2023-03-02 2025-09-11\n",
      "5  2022-05-08 2025-09-11\n",
      "6  2011-06-04 2025-09-11\n",
      "7  2012-03-05 2025-09-11\n",
      "8  2017-12-08 2025-09-11\n",
      "         起始日期       结束日期  间隔天数\n",
      "序号                            \n",
      "1  2004-05-06 2025-09-11  7798\n",
      "2  2004-07-08 2025-09-11  7735\n",
      "3  2024-09-06 2025-09-11   370\n",
      "4  2023-03-02 2025-09-11   924\n",
      "5  2022-05-08 2025-09-11  1222\n",
      "6  2011-06-04 2025-09-11  5213\n",
      "7  2012-03-05 2025-09-11  4938\n",
      "8  2017-12-08 2025-09-11  2834\n"
     ]
    }
   ],
   "execution_count": 36
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
